﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.Bll.Com;
using SmallERP.COM;
using SmallERP.DAL;
using SmallERP.MODEL.FoundationModel;
using System.Data;

namespace SmallERP.Bll.WastePaperManage
{
    public class WastePaperManageBLL
    {
        /// <summary>
        /// 取得所有的未确认的浪费记录
        /// </summary>
        /// <returns></returns>
        public List<PaperWasteRecord_info> GetUnSureWasteRecord()
        {

            List<PaperWasteRecord_info> rtnList = new List<PaperWasteRecord_info>();
            // PaperWasteRecord_info recordInfo;
            int unSureStatus = (int)Status.WasterPaperStatus.unSure;
            //using (smallerpDataContext db = new smallerpDataContext())
            //{
            //    //recordInfo=new PaperWasteRecord_info();
            //    List<PaperWasteRecords> records = db.PaperWasteRecords.Where(p => p.Status == unSureStatus).ToList();
            //    foreach (PaperWasteRecords list in records)
            //    {
            //        recordInfo = new PaperWasteRecord_info();
            //        recordInfo.Id = list.Id;
            //        recordInfo.CreateTime = list.CreateTime;
            //        recordInfo.Length = list.Length;
            //      list.PaperInfoReference.Load();
            //         PaperInfo_info paperinfo=new PaperInfo_info();

            //        {
            //            paperinfo.AddTime = list.PaperInfo.AddTime;
            //            paperinfo.EndPaperLength = list.PaperInfo.EndPaperLength;
            //            paperinfo.InvalidationArea = list.PaperInfo.InvalidationArea;
            //            paperinfo.LastBordId = list.PaperInfo.LastBordId;
            //            paperinfo.LastLength = list.PaperInfo.LastLength;
            //            paperinfo.LastPrintTime = list.PaperInfo.LastPrintTime;
            //            paperinfo.Length = list.PaperInfo.Length;
            //            ;
            //            paperinfo.PaperConsumptionLength = list.PaperInfo.PaperConsumptionLength;

            //            paperinfo.PaperEndArea = list.PaperInfo.PaperEndArea;
            //            paperinfo.PrintPaperId = list.PaperInfo.PrintPaperId;
            //        }
            //        recordInfo.PaperInfo = paperinfo;
            //        if (!list.PaperWasteTypeReference.IsLoaded)
            //        {
            //            list.PaperWasteTypeReference.Load();
            //        }
            //        recordInfo.PaperWasteType = new PaperWasteType_info() { Id = list.PaperWasteType.Id, PaperOperationTypeName = list.PaperWasteType.PaperOperationTypeName };
            //        recordInfo.Remark = list.Remark;
            //        recordInfo.Status = list.Status;
            //        recordInfo.Width = list.Width;

            //        rtnList.Add(recordInfo);
            //    }

            string QueryInfo = "select Id,PaperWasteRecords.PaperWasteTypeId,PaperInfo.PrintPaperId,PaperWasteRecords.Length as pwrLength,PaperWasteRecords.Width as pwrWidth,CreateTime"
            + ",PaperWasteRecords.Status as pwrStatus,PaperWasteRecords.Remark as pwrRemark,AddTime,EndPaperLength,InvalidationArea,LastBordId,LastLength, "
            + "LastPrintTime,PaperInfo.Length as piLength,PaperConsumptionLength,PaperEndArea,PrintPaperId from PaperWasteRecords,PaperInfo where PaperWasteRecords.PrintPaperId=PaperInfo.PrintPaperId and PaperWasteTypeId=PaperWasteType.id and status=" + unSureStatus;
            DataTable table = AccessHelper.dataTable(QueryInfo);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                PaperWasteRecord_info recordInfo = new PaperWasteRecord_info();
                recordInfo.CreateTime = Convert.ToDateTime(table.Rows[i]["CreateTime"]);
                recordInfo.Id = Convert.ToInt32(table.Rows[i]["Id"]);
                recordInfo.Length = Convert.ToDouble(table.Rows[i]["pwrLength"]);
                recordInfo.Remark = Convert.ToString(table.Rows[i]["pwrRemark"]);
                recordInfo.Status = Convert.ToInt32(table.Rows[i]["pwrStatus"]);
                recordInfo.Width = Convert.ToDouble(table.Rows[i]["pwrWidth"]);
                recordInfo.PaperInfo = new PaperInfo_info()
                {
                    AddTime = Convert.ToDateTime(table.Rows[i]["AddTime"]),
                    EndPaperLength = Convert.ToDouble(table.Rows[i]["EndPaperLength"]),
                    InvalidationArea = Convert.ToDouble(table.Rows[i]["InvalidationArea"]),
                    LastBordId = Convert.ToInt32(table.Rows[i]["LastBordId"]),
                    LastLength = Convert.ToDouble(table.Rows[i]["LastLength"]),
                    LastPrintTime = Convert.ToDateTime(table.Rows[i]["LastPrintTime"]),
                    Length = Convert.ToDouble(table.Rows[i]["piLength"]),
                    PaperConsumptionLength = Convert.ToDouble(table.Rows[i]["PaperConsumptionLength"]),
                    PaperEndArea = Convert.ToDouble(table.Rows[i]["PaperEndArea"]),
                    PrintPaperId = Convert.ToInt32(table.Rows[i]["PrintPaperId"])
                };
                rtnList.Add(recordInfo);

            }

            return rtnList;
        }
        /// <summary>
        /// 取得时间段内所有的未确认的浪费记录
        /// </summary>
        /// <returns></returns>
        public List<PaperWasteRecord_info> GetUnSureWasteRecord(DateTime inDateStart, DateTime inDateEnd)
        {

            List<PaperWasteRecord_info> rtnList = new List<PaperWasteRecord_info>();
            //PaperWasteRecord_info recordInfo;
            int unSureStatus = (int)Status.WasterPaperStatus.unSure;
            string QueryInfo = "select Id,PaperWasteRecords.PaperWasteTypeId,PaperInfo.PrintPaperId,PaperWasteRecords.Length as pwrLength,PaperWasteRecords.Width as pwrWidth,CreateTime"
            + ",PaperWasteRecords.Status as pwrStatus,PaperWasteRecords.Remark as pwrRemark,AddTime,EndPaperLength,InvalidationArea,LastBordId,LastLength, "
            + "LastPrintTime,PaperInfo.Length as piLength,PaperConsumptionLength,PaperEndArea,PrintPaperId from PaperWasteRecords,PaperInfo where PaperWasteRecords.PrintPaperId=PaperInfo.PrintPaperId and PaperWasteTypeId=PaperWasteType.id and status=" + unSureStatus
            + " and createtime<#" + inDateEnd + "# and createtime>#" + inDateStart+"#";
            DataTable table = AccessHelper.dataTable(QueryInfo);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                PaperWasteRecord_info recordInfo = new PaperWasteRecord_info();
                recordInfo.CreateTime = Convert.ToDateTime(table.Rows[i]["CreateTime"]);
                recordInfo.Id = Convert.ToInt32(table.Rows[i]["Id"]);
                recordInfo.Length = Convert.ToDouble(table.Rows[i]["pwrLength"]);
                recordInfo.Remark = Convert.ToString(table.Rows[i]["pwrRemark"]);
                recordInfo.Status = Convert.ToInt32(table.Rows[i]["pwrStatus"]);
                recordInfo.Width = Convert.ToDouble(table.Rows[i]["pwrWidth"]);
                recordInfo.PaperInfo = new PaperInfo_info()
                {
                    AddTime = Convert.ToDateTime(table.Rows[i]["AddTime"]),
                    EndPaperLength = Convert.ToDouble(table.Rows[i]["EndPaperLength"]),
                    InvalidationArea = Convert.ToDouble(table.Rows[i]["InvalidationArea"]),
                    LastBordId = Convert.ToInt32(table.Rows[i]["LastBordId"]),
                    LastLength = Convert.ToDouble(table.Rows[i]["LastLength"]),
                    LastPrintTime = Convert.ToDateTime(table.Rows[i]["LastPrintTime"]),
                    Length = Convert.ToDouble(table.Rows[i]["piLength"]),
                    PaperConsumptionLength = Convert.ToDouble(table.Rows[i]["PaperConsumptionLength"]),
                    PaperEndArea = Convert.ToDouble(table.Rows[i]["PaperEndArea"]),
                    PrintPaperId = Convert.ToInt32(table.Rows[i]["PrintPaperId"])
                };
                rtnList.Add(recordInfo);

            }

            return rtnList;
           
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="inWasteId"></param>
        /// <returns></returns>
        public int MakeSureRecordSinger(int inWasteId)
        {
            try
            {


                //using (smallerpDataContext db = new smallerpDataContext())
                //{
                //    PaperWasteRecords list = db.PaperWasteRecords.FirstOrDefault(p => p.Id == inWasteId);
                //    if (list == null)
                //    {
                //        return 0;
                //    }

                //    if (list.Status != (int)Status.WasterPaperStatus.Sure)
                //    {
                //        list.Status = (int)Status.WasterPaperStatus.Sure;
                //    }
                //    else
                //    {
                //        return 2;
                //    }

                //    db.SaveChanges();

                //}
                string upPaperWasteRecords = "update PaperWasteRecords set Status=" + (int)Status.WasterPaperStatus.Sure;
                AccessHelper.ExecuteNonQuery(upPaperWasteRecords);
            }
            catch
            {

                return -1;
            }
            return 1;

        }
    }
}
